package com.gopay.bis.cmb.bcdl.operation;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import com.gopay.bis.cmb.bcdl.operation.doc.CmbAgentPayBean;
import com.gopay.bis.cmb.bcdl.operation.doc.CmbQueryBalance;
import com.gopay.bis.cmb.bcdl.operation.doc.CmbXmlBean;
import com.gopay.bis.cmb.bcdl.operation.doc.DcopdPayx;
import com.gopay.bis.cmb.bcdl.operation.doc.Info;
import com.gopay.bis.cmb.bcdl.operation.doc.SdkAcInfx;
import com.gopay.bis.cmb.bcdl.operation.doc.SdkAtdQyx;
import com.gopay.bis.cmb.bcdl.operation.doc.SdkAtdRqx;
import com.gopay.bis.cmb.bcdl.operation.doc.SdkAtsQyx;
import com.gopay.bis.cmb.bcdl.operation.doc.SdkAtsRqx;
import com.gopay.bis.cmb.bcdl.operation.doc.SdkPayRqx;
import com.gopay.bis.cmb.bcdl.operation.doc.SdkQueryRqx;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.bank.CmbConstants;
import com.gopay.common.domain.bank.cmb.bcdl.CmbBcdlMessage;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.CompactWriter;

public class Transformer {
	private static final String DATETYPE = "2";
	private static final String PAY_FUNNAM = "DCPAYMNT";
	private static final String QUERY_FUNNAM = "GetPaymentInfo";
	private static final String AGENTPAY_FUNNAM = "AgentRequest";
	private static final String AGENTQUERY_FUNNAM = "GetAgentInfo";
	private static final String AGENTQUERYDETAIL_FUNNAM = "GetAgentDetail";
	private static final String AGENTQUERYACC_FUNNAM = "GetAccInfo";
	private static final String AGENTBUSCOD = "N03020";
	private static final String BUSCOD = "N02031";
	private String payAcct;
	private static final String DBTBBK = "10";
	private static final String CCYNBK = "10";

	public String doFormat(CmbBcdlMessage msg, String loginName) {
		CmbXmlBean xmlBean = new CmbXmlBean();
		Info info = new Info();
		info.setDatTyp(DATETYPE);
		info.setFunnam(PAY_FUNNAM);
		info.setLgnNam(loginName);
		xmlBean.setInfo(info);
		SdkPayRqx sdkPayRqx = new SdkPayRqx();
		sdkPayRqx.setBusCod(BUSCOD);
		xmlBean.setSdkPayRqx(sdkPayRqx);
		DcopdPayx dcopdPayx = new DcopdPayx();
		dcopdPayx.setYurref(msg.getOrderId());
		dcopdPayx.setDbtacc(payAcct);
		dcopdPayx.setDbtbbk(DBTBBK);
		dcopdPayx.setTrsamt(msg.getAmount().toString());
		dcopdPayx.setCcynbk(CCYNBK);
		if (msg.getEmergency() == 0) {
			dcopdPayx.setStlchn("F");
		} else {
			dcopdPayx.setStlchn("N");
		}
		dcopdPayx.setNusage(msg.getRemark());
		dcopdPayx.setCrtacc(msg.getRecvBankAcctNum());
		dcopdPayx.setCrtnam(msg.getRecvBankAcctName());
		dcopdPayx.setCrtbnk(msg.getPbocBankBranchName());
		dcopdPayx.setBnkflg(msg.getBnkflg());
		xmlBean.setDcopdPayx(dcopdPayx);
		XStream xs = new XStream();
		xs.autodetectAnnotations(true);
		xs.processAnnotations(CmbXmlBean.class);
		OutputStream output = new ByteArrayOutputStream();
		xs.marshal(xmlBean, new CompactWriter(new OutputStreamWriter(output)));
		String plainStr = output.toString();
		return plainStr;
	}
	public String doAgentPayFormat(CmbBcdlMessage msg, String loginName) {
		CmbAgentPayBean xmlBean = new CmbAgentPayBean();
		Info info = new Info();
		info.setDatTyp(DATETYPE);
		info.setFunnam(AGENTPAY_FUNNAM);
		info.setLgnNam(loginName);
		xmlBean.setInfo(info);
		SdkAtsRqx sdkAtsRqx = new SdkAtsRqx();
		sdkAtsRqx.setBusCod(AGENTBUSCOD);
		sdkAtsRqx.setBusMod("00002");
		sdkAtsRqx.setTrsTyp("BYBK");
		sdkAtsRqx.setcTrsTyp("代发其他");
		sdkAtsRqx.setDbtacc(payAcct);
		sdkAtsRqx.setBbknbr(DBTBBK);
		sdkAtsRqx.setCcynbk(CCYNBK);
		sdkAtsRqx.setSum(msg.getAmount().toString());
		sdkAtsRqx.setTotal("1");
		sdkAtsRqx.setYurref(msg.getOrderId());
		sdkAtsRqx.setMemo(msg.getRemark());
		xmlBean.setSdkAtsRqx(sdkAtsRqx);
		SdkAtdRqx sdkAtdRqx = new SdkAtdRqx();
	
		sdkAtdRqx.setAccNbr(msg.getRecvBankAcctNum());
		sdkAtdRqx.setCltNam(msg.getRecvBankAcctName());
		sdkAtdRqx.setTrsAmt(msg.getAmount().toString());
		sdkAtdRqx.setTrsdsp("");
		sdkAtdRqx.setBnkflg(msg.getBnkflg());
		xmlBean.setSdkAtdRqx(sdkAtdRqx);
		XStream xs = new XStream();
		xs.autodetectAnnotations(true);
		xs.processAnnotations(CmbAgentPayBean.class);
		OutputStream output = new ByteArrayOutputStream();
		xs.marshal(xmlBean, new CompactWriter(new OutputStreamWriter(output)));
		String plainStr = output.toString();
		return plainStr;
	}
	public String doQueryFormat(CmbBcdlMessage msg, String loginName) {
		CmbXmlBean xmlBean = new CmbXmlBean();
		Info info = new Info();
		info.setDatTyp(DATETYPE);
		info.setFunnam(QUERY_FUNNAM);
		info.setLgnNam(loginName);
		xmlBean.setInfo(info);
		SdkQueryRqx sdkQueryRqx = new SdkQueryRqx();
		sdkQueryRqx.setBusCod(BUSCOD);
		sdkQueryRqx.setYurref(msg.getOrderId());
		sdkQueryRqx.setBgnDat(msg.getOrderDate());
		sdkQueryRqx.setEndDat(msg.getOrderDate());
		/*sdkQueryRqx.setBgnDat("20140917");
		sdkQueryRqx.setEndDat("20141009");*/
		xmlBean.setSdkQueryRqx(sdkQueryRqx);
		XStream xs = new XStream();
		xs.autodetectAnnotations(true);
		xs.processAnnotations(CmbXmlBean.class);
		OutputStream output = new ByteArrayOutputStream();
		xs.marshal(xmlBean, new CompactWriter(new OutputStreamWriter(output)));
		String plainStr = output.toString();
		return plainStr;
	}
	public String doAgentQueryFormat(CmbBcdlMessage msg, String loginName) {
		CmbAgentPayBean xmlBean = new CmbAgentPayBean();
		Info info = new Info();
		info.setDatTyp(DATETYPE);
		info.setFunnam(AGENTQUERY_FUNNAM);
		info.setLgnNam(loginName);
		xmlBean.setInfo(info);
		SdkAtsQyx sdkAtsQyx = new SdkAtsQyx();
		sdkAtsQyx.setBusCod(AGENTBUSCOD);
		sdkAtsQyx.setYurref(msg.getOrderId());
		sdkAtsQyx.setBgnDat(msg.getOrderDate());
		sdkAtsQyx.setEndDat(msg.getOrderDate());
		/*sdkQueryRqx.setBgnDat("20140917");
		sdkQueryRqx.setEndDat("20141009");*/
		xmlBean.setSdkAtsQyx(sdkAtsQyx);
		XStream xs = new XStream();
		xs.autodetectAnnotations(true);
		xs.processAnnotations(CmbAgentPayBean.class);
		OutputStream output = new ByteArrayOutputStream();
		xs.marshal(xmlBean, new CompactWriter(new OutputStreamWriter(output)));
		String plainStr = output.toString();
		return plainStr;
	}
	
    public String doQueryBalanceFormat(CmbBcdlMessage msg,String loginName) {
        CmbQueryBalance xmlBean = new CmbQueryBalance();
        Info info = new Info();
        info.setDatTyp(DATETYPE);
        info.setFunnam(AGENTQUERYACC_FUNNAM);
        info.setLgnNam(loginName);
        xmlBean.setInfo(info);
        SdkAcInfx sdkAcInfx = new SdkAcInfx();
        sdkAcInfx.setBbkNbr("10"); // 分行号北京
        sdkAcInfx.setAccNbr(payAcct);
        xmlBean.setSdkAcInfx(sdkAcInfx);
        XStream xs = new XStream();
        xs.autodetectAnnotations(true);
        xs.processAnnotations(CmbQueryBalance.class);
        OutputStream output = new ByteArrayOutputStream();
        xs.marshal(xmlBean, new CompactWriter(new OutputStreamWriter(output)));
        String plainStr = output.toString();
        return plainStr;
    }
	
    public CmbBcdlMessage agentPayParse(String xml) {
	    CmbBcdlMessage msg = new CmbBcdlMessage();
		CmbAgentPayBean xmlBean = new CmbAgentPayBean();
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);
		xstream.processAnnotations(CmbAgentPayBean.class);
		xmlBean = (CmbAgentPayBean) xstream.fromXML(xml);
		msg.setRetCode(xmlBean.getInfo().getRetCod());
		if(CmbConstants.RET_SUC.equals(xmlBean.getInfo().getRetCod())){
		    msg.setFlag(true);
		}else{
		    msg.setFlag(false);
		}
		return msg;
	}
	public CmbBcdlMessage parse(String xml) {
        CmbBcdlMessage msg = new CmbBcdlMessage();
		CmbXmlBean xmlBean = new CmbXmlBean();
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);
		xstream.processAnnotations(CmbXmlBean.class);
		xmlBean = (CmbXmlBean) xstream.fromXML(xml);
		msg.setRetCode(xmlBean.getInfo().getRetCod());
		
		if(xmlBean.getNtqPayRqz()!=null){
			//add by zyt,2017-10-23,返回给bis的时候，传入下面两个参数
			msg.setReqStatus(xmlBean.getNtqPayRqz().getReqSts());
			msg.setRetCode(xmlBean.getNtqPayRqz().getRtnFlg());
			if(xmlBean.getNtqPayRqz().getErrTxt()!=null){
				msg.setRetMsg(xmlBean.getNtqPayRqz().getErrTxt());
			}
		}
		if(CmbConstants.RET_SUC.equals(xmlBean.getInfo().getRetCod())&&CmbConstants.FIN.equals(xmlBean.getNtqPayRqz().getReqSts())&&CmbConstants.FAIL.equals(xmlBean.getNtqPayRqz().getRtnFlg())){
		    msg.setFlag(false);
		}else{
		    msg.setFlag(true);
		}
		return msg;
	}
	
	public CmbBcdlMessage doQueryParse(String xml,CmbBcdlMessage msg) {
		CmbXmlBean xmlBean = new CmbXmlBean();
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);
		xstream.ignoreUnknownElements();
		xstream.processAnnotations(CmbXmlBean.class);
		xmlBean = (CmbXmlBean) xstream.fromXML(xml);
		if(xmlBean.getNtqPayQyz()!=null){
			msg.setRetCode(xmlBean.getNtqPayQyz().getRtnFlg());
			msg.setReqStatus(xmlBean.getNtqPayQyz().getReqSts());
			msg.setRetMsg(xmlBean.getNtqPayQyz().getRtnNar());
		}else{
            String retCode = TransUtils.getMsgField("<RETCOD>", "</RETCOD>", xml);
            msg.setRetCode(retCode);
		}
		return msg;
	}
	
	public CmbBcdlMessage doAgentQueryParse(String xml,CmbBcdlMessage msg) {
		CmbAgentPayBean xmlBean = new CmbAgentPayBean();
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);
		xstream.ignoreUnknownElements();
		xstream.processAnnotations(CmbAgentPayBean.class);
		xmlBean = (CmbAgentPayBean) xstream.fromXML(xml);
		
		if(xmlBean.getNtqAtsQyz()!=null){
			msg.setRetCode(xmlBean.getNtqAtsQyz().getRtnFlg());
			msg.setReqStatus(xmlBean.getNtqAtsQyz().getReqSta());
			msg.setRetMsg(xmlBean.getNtqAtsQyz().getErrdsp());
			msg.setReqNbr(xmlBean.getNtqAtsQyz().getReqNbr());
		}else{
		    String retCode = TransUtils.getMsgField("<RETCOD>", "</RETCOD>", xml);
            msg.setRetCode(retCode);
		}
		
		return msg;
	}

	
	
    public CmbBcdlMessage doQueryBalanceParse(String xml, CmbBcdlMessage msg) {
        CmbQueryBalance xmlBean = new CmbQueryBalance();
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.ignoreUnknownElements();
        xstream.processAnnotations(CmbQueryBalance.class);
        xmlBean = (CmbQueryBalance) xstream.fromXML(xml);

        msg.setRetCode(xmlBean.getInfo().getRetCod());
        if(CmbConstants.RET_SUC.equals(xmlBean.getInfo().getRetCod())){
            msg.setBalance(xmlBean.getNtQacInfz().getAvlBlv());
            msg.setPayAccNum(xmlBean.getNtQacInfz().getAccNbr());
        }else{
            msg.setRetMsg(xmlBean.getInfo().getErrMsg());
        }
        
        return msg;
    }
    public String doAgentQueryDetailFormat(CmbBcdlMessage msg, String loginName) {

	
		CmbAgentPayBean xmlBean = new CmbAgentPayBean();
		Info info = new Info();
		info.setDatTyp(DATETYPE);
		info.setFunnam(AGENTQUERYDETAIL_FUNNAM);
		info.setLgnNam(loginName);
		xmlBean.setInfo(info);
		SdkAtdQyx sdkAtdQyx = new SdkAtdQyx();
		sdkAtdQyx.setReqNbr(msg.getReqNbr());
		xmlBean.setSdkAtdQyx(sdkAtdQyx);
		XStream xs = new XStream();
		xs.autodetectAnnotations(true);
		xs.processAnnotations(CmbAgentPayBean.class);
		OutputStream output = new ByteArrayOutputStream();
		xs.marshal(xmlBean, new CompactWriter(new OutputStreamWriter(output)));
		String plainStr = output.toString();
		return plainStr;
	}
	public String doAgentDetailQueryParse(String xml) {

		CmbAgentPayBean xmlBean = new CmbAgentPayBean();
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);
		xstream.ignoreUnknownElements();
		xstream.processAnnotations(CmbAgentPayBean.class);
		xmlBean = (CmbAgentPayBean) xstream.fromXML(xml);
		String errMsg ="";
		if(xmlBean.getNtqAtdQyz()!=null){
			errMsg=xmlBean.getNtqAtdQyz().getErrrDsp();
		}
		
		return errMsg;
	}
	public String getPayAcct() {
		return payAcct;
	}

	public void setPayAcct(String payAcct) {
		this.payAcct = payAcct;
	}

}
